import React, { Component } from 'react';
import { inject, observer } from 'mobx-react';
import { WeaLocaleProvider, WeaRightMenu, WeaDialog } from 'ecCom';
import { toJS } from 'mobx';
import * as dialogTypes from '../../../constants/OdocCompsDialogType';
import { Button } from 'antd';
import SignInput from '../public/SignInput';
import SignLogTable from './SignLogTable';
import '../public/signUpdateLog.less';

const { getLabel } = WeaLocaleProvider;

/**
 * 修改流程意见
 * 加操作者，或者减少操作者，或者修改状态
 */
@inject('updateSignLogStore', 'odocCompsStore')
@observer
class Index extends Component {
  constructor(props) {
    super(props);
    this.state = {
      height: 500,
      visible: true,
    };
  }

  componentWillReceiveProps(nextProps) {
    if (this.props.visible != nextProps.visible && nextProps.visible) {
      this.loadData();
    }
  }

  componentWillMount() {
    this.loadData();
  }

  loadData() {
    const { updateSignLogStore, odocCompsStore } = this.props;
    const { initCoreParams } = updateSignLogStore;
    let params = {};
    try {
      let { requestParam } = odocCompsStore;
      requestParam = toJS(requestParam);
      delete requestParam.markInfo;
      params = { ...params, ...requestParam };
    } catch (e) {
      // 异常处理，用于热部署
      params = { requestid: 12623619, workflowid: 51022, nodeid: 42530 };
    }
    initCoreParams(params);
  }

  render() {
    const { updateSignLogStore, odocCompsStore = {} } = this.props;
    const { height } = this.state;
    let visible = this.state.visible;
    try {
      let { dialogVisible } = odocCompsStore;
      visible = dialogVisible.get(dialogTypes.ODOC_UPDATE_SIGNLOG);
    } catch (e) {
      visible = true;
    }

    const { coreparams = {} } = updateSignLogStore;
    const { title = getLabel(132410, '修改意见') } = coreparams;
    let dialogparams = {
      style: { width: 1000, height },
      visible,
      title,
      icon: 'icon-coms-Need-feedback',
      onCancel: this.onCancel,
      buttons: this.getButtons(this.allBtns()),
      layout: jQuery('.wf-req')[0],
    };

    return (
      <div>
        <WeaDialog ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@18uj3i`} {...dialogparams} className={'signUpdateDialog'}>
          <WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@5urt19`} datas={this.getRightMenu()}>
            <div className={'content'}>
              {this.getFormContent()}
              {this.getSignInputContent()}
            </div>
          </WeaRightMenu>
        </WeaDialog>
      </div>
    );
  }

  getFormContent = () => <SignLogTable ecId={`${this && this.props && this.props.ecId || ''}_SignLogTable@njnuvr`} {...this.props} />

	/**
	 * 获取签字意见框组件
	 * @returns {*}
	 */
  getSignInputContent = () => {
    const { odocCompsStore = {}, updateSignLogStore } = this.props;
    const { workflowStores = {} } = odocCompsStore;
    let { refid = 'update', signinput = {}, showSignInput = false, closeShowSignInput, titleparams = {} } = updateSignLogStore;
    signinput = toJS(signinput);
    const { nodeidspan, nodeid, operatorspan, operator } = titleparams;
    let title = getLabel(17614, '签字意见');
    if ((operatorspan || operator) && (nodeidspan || nodeid)) {
      title += ` : ${operatorspan || operator}-${nodeidspan || nodeid}`;
    }
    // 弹出签字意见框
    let dialogparams = {
      style: { width: 800, height: 300 },
      visible: showSignInput,
      title: <span dangerouslySetInnerHTML={{ __html: title }} />,
      icon: 'icon-coms-Need-feedback',
      onCancel: closeShowSignInput,
      buttons: this.getButtons(this.allSignBtns()),
      layout: jQuery('.wf-req')[0],
    };
    return (<WeaDialog ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@ac2wwo`} {...dialogparams}>
      <SignInput ecId={`${this && this.props && this.props.ecId || ''}_SignInput@gre0r7`}
        coreparams={{
          title: getLabel(103, '修改'),
          refid,
          premarkinfo: signinput, // TODO 注意这里仅选择一个数据

        }}
        workflowStores={workflowStores}
        allFunction={{ changecallback: this.changeSignInput }}
      />
    </WeaDialog>);
  }

	/**
	 * 签字意见修改
	 * @param v
	 */
  changeSignInput = (v) => {
    const { updateSignLogStore } = this.props;
    const { changeSubmitParams, changeSignInput } = updateSignLogStore;
    changeSubmitParams(v);
    changeSignInput(v);
  }

	/**
	 * 取消
	 * @param res
	 */
  onCancel = () => {
    const { updateSignLogStore, onClose, odocCompsStore } = this.props;
    const { destory, savenumber = 0 } = updateSignLogStore;
    if (savenumber > 0) {
      const { dialogParam = {} } = odocCompsStore;
      const param = dialogParam[dialogTypes.ODOC_UPDATE_SIGNLOG] || {};
      const { onSuccess } = param;
      typeof onSuccess == 'function' && (onSuccess());
    }
    typeof onClose == 'function' && (onClose());
    typeof destory == 'function' && (destory());
  }

	/**
	 * 获取右键菜单
	 * @returns {Array}
	 */
  getRightMenu() {
    const { updateSignLogStore } = this.props;
    const { loading = false } = updateSignLogStore;
    let btns = [];
    const showAllBtns = this.allBtns();
    showAllBtns && showAllBtns.map((item) => {
      if (item.key != 'close') {
        if (!loading) {
          item.disabled = 'true';
        }
        btns.push({ ...item });
      } else {
        btns.push({ ...item });
      }
    });
    return btns;
  }

	/**
	 * dialog获取底部按钮
	 * @returns {Array}
	 */
  getButtons = (showAllBtns = []) => {
    let buttons = [];
    const { updateSignLogStore } = this.props;
    const { loading = false } = updateSignLogStore;
    showAllBtns && showAllBtns.map((item) => {
      if (item.key != 'close') {
        if (!loading) {
          item.disabled = 'true';
        }
        buttons.push([<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@363ndd@${item.key}`} {...item}>{item.content}</Button>]);
      } else {
        buttons.push([<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@zgcb8l@${item.key}`} {...item}>{item.content}</Button>]);
      }
    });
    return buttons;
  }

	/**
	 * 统一管理btns
	 * @returns {{key: string, icon: *, content: *, onClick: Index.goSave, type: string}[]}
	 */
  allBtns = () => {
    const { pluginbuttons = [] } = this.props;
    let showAllBtns = [
			/* {
					 key: "save", icon: <i className='icon-coms-Preservation'/>,
					 content: getLabel(826, '确定'),
					 onClick: () => doSave(workflowStores, this.onCancel),
					 type: "primary"
			 }, */
      {
        key: 'close',
        icon: <i className="icon-coms-go-back" />,
        content: getLabel(201, '取消'),
        onClick: this.onCancel,
        type: 'ghost',
      },
    ];
    pluginbuttons && pluginbuttons.length >= 1 && (showAllBtns = showAllBtns.concat(pluginbuttons));
    return showAllBtns;
  }

	/**
	 * 弹出签字意见框对应的按钮
	 * @returns {*[]}
	 */
  allSignBtns = () => {
    const { updateSignLogStore, pluginbuttons = [], odocCompsStore = {} } = this.props;
    const { workflowStores = {} } = odocCompsStore;
    const { doSave, closeShowSignInput } = updateSignLogStore;
    let showAllBtns = [
      {
        key: 'save',
        icon: <i className="icon-coms-Preservation" />,
        content: getLabel(826, '确定'),
        onClick: () => doSave(workflowStores, closeShowSignInput),
        type: 'primary',
      },
      {
        key: 'close',
        icon: <i className="icon-coms-go-back" />,
        content: getLabel(201, '取消'),
        onClick: closeShowSignInput,
        type: 'ghost',
      },
    ];
    pluginbuttons && pluginbuttons.length >= 1 && (showAllBtns = showAllBtns.concat(pluginbuttons));
    return showAllBtns;
  }
}

export default Index;
